package com.lg.atp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.atp.dao.RoleDao;
import com.lg.atp.entity.TblRoleInfo;
import com.lg.atp.entity.TblRoleMenuButtonInfo;
import com.lg.atp.service.IMenuRoleService;
import com.lg.atp.service.IRoleService;
import com.lg.atp.service.IUserRoleService;


@Service("iRoleService")
public class RoleServiceImpl implements IRoleService{

	
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private IMenuRoleService iMenuRoleService;
	@Autowired
	private IUserRoleService iUserRoleService;
	
	
	@Override
	public TblRoleInfo findRoleById(Long id) {
		return roleDao.findRoleById(id);
	}

	@Override
	public List<TblRoleInfo> findUserRole(Long userId) {
		return roleDao.findUserRole(userId);
	}

	@Transactional
	@Override
	public void insertRole(TblRoleInfo role) {
		roleDao.insertRole(role);
		String menuIds = role.getMenuIds();
		if(StringUtils.isNotBlank(menuIds)){
			List<TblRoleMenuButtonInfo> rms = new ArrayList<TblRoleMenuButtonInfo>();
			String[] mids = menuIds.split(",");
			Long roleId = role.getId();
			for(String menuId:mids){
				TblRoleMenuButtonInfo roleMenu = new TblRoleMenuButtonInfo();
				roleMenu.setMenuButtonId(Long.valueOf(menuId));
				roleMenu.setRoleId(roleId);
				rms.add(roleMenu);
			}
			iMenuRoleService.insertMenuRole(rms);
				
		}
	}

	@Transactional
	@Override
	public void updateRole(TblRoleInfo role) {
		roleDao.updateRole(role);
		iMenuRoleService.delMenuByRoleId(role.getId());
		String menuIds = role.getMenuIds();
		if(StringUtils.isNotBlank(menuIds)){
			List<TblRoleMenuButtonInfo> rms = new ArrayList<TblRoleMenuButtonInfo>();
			String[] mids = menuIds.split(",");
			Long roleId = role.getId();
			for(String menuId:mids){
				TblRoleMenuButtonInfo roleMenu = new TblRoleMenuButtonInfo();
				roleMenu.setMenuButtonId(Long.valueOf(menuId));
				roleMenu.setRoleId(roleId);
				rms.add(roleMenu);
			}
			iMenuRoleService.insertMenuRole(rms);
				
		}
	}

	@Override
	public List<TblRoleInfo> getRoleByMenuId(Long menuId) {
		return roleDao.getRoleByMenuId(menuId);
	}

	@Override
	public PageInfo<TblRoleInfo> getAllRole(Integer pageIndex,Integer pageSize) {
		if(pageIndex != null && pageSize != null)
		PageHelper.startPage(pageIndex, pageSize);
		List<TblRoleInfo> list =  roleDao.getAllRole();
		PageInfo<TblRoleInfo> pageInfo = new PageInfo<TblRoleInfo>(list);
		return pageInfo;
	}

	@Transactional
	@Override
	public void deleteRoles(List<Long> roleIds) {
		if(roleIds != null && !roleIds.isEmpty()){
			roleDao.delRole(roleIds);
			iMenuRoleService.delMenuByRoleIds(roleIds);
			iUserRoleService.delUserRoleByRoleId(roleIds);
		}
	}

}
